﻿using Engine;
using Engine.Media;
using Game;
namespace Mekiasm
{
    public class TreeTap : ItemFlat
    {
        public static int Index;

        public TreeTap() : base("木龙头", MekiasmInit.GetFlatTexture(MekiasmInit.R2, "treetap"))
        {
            
            
        }

        public override string GetDescription(int value)
        {
            return "木龙头主要用于在橡胶树的橡胶口上采集粘性树脂，一次采集会掉落出1~3个粘性树脂。\n如果对准橡胶树上没有橡胶孔的地方采集，也会消耗耐久度。\n次要用途是用于从酒桶中取酒，方法是在身上有石杯的情况下，拿着木空头对着酒桶右键即可";
        }

        public override float GetBlockHealth(int value)
        {
            SaveItemSystem.GetBlockData(value, out var data);
            if (data == null) return -1f;
            else return data.fuelAmount / (float)GetMaxPower();
        }


        public override int GetBasePower()
        {
            return 32;
        }

        public override int GetMaxPower()
        {
            return 32;
        }

        public override int GetCraftedResultValue(int value)
        {
            return SaveItemSystem.StoreBlockData(value, GetBasePower());
        }

        public override Vector3 GetIconViewOffset(int value, DrawBlockEnvironmentData environmentData)
        {
            return new Vector3(0f, 0f, 1f);
        }

        public override string GetCategory(int value)
        {
            return MekiasmInit.categories_base_item;
        }

        public override bool OnUse(Ray3 aim, ComponentMiner componentMiner, TerrainRaycastResult? raycastResult)
        {
            if (raycastResult.HasValue)
            {
                if (IsBlockEqual(RubberWood.Index, raycastResult.Value.Value) && ILibrary.getExtraData(raycastResult.Value.Value) > 0)
                {
                    ILibrary.SubsystemTerrain.ChangeCell(raycastResult.Value.CellFace.X, raycastResult.Value.CellFace.Y, raycastResult.Value.CellFace.Z, RubberWood.Index);
                    SubsystemPickables subsystemPickables = componentMiner.Project.FindSubsystem<SubsystemPickables>();
                    subsystemPickables.AddPickable(Resin.Index, random.Int(1, 3), new Vector3(raycastResult.Value.CellFace.Point) + new Vector3(0.5f), null, null);
                    var s = ContentManager.Get<StreamingSource>("Mekiasm/sounds/Treetap");
                    new Engine.Audio.StreamingSound(s).Play();
                    SaveItemSystem.ChangePower(componentMiner.Inventory, componentMiner.Inventory.ActiveSlotIndex, -1);
                }
            }
            return true;
        }
    }
}
